const axios =require('axios');
function bigReq(tryTimes, ...args) {
  return new Promise((resolve, reject) => {
    let currentTryTimes = 0
    let res = {
      success: [],
      error: []
    };
    (function handleAllReq(tryTimes, ...args) {
      if (currentTryTimes !== 0) {
        console.log("当前重试次数：", currentTryTimes);
      }
      let length = args.length
      let successIndex = []
      let pa = Promise.allSettled(args.map(item => item()))
      pa.then((v) => {
        v.forEach((item, index) => {
          if (item.status === 'fulfilled') {
            res.success.push(item.value.data.data)
            successIndex.push(index)
          }
        })
        if (res.success.length === length) {
          console.log("请求全部成功!");
          resolve(res);
        } else {
          if (currentTryTimes < tryTimes) {
            let arr = args.filter((item, index) => {
              return !successIndex.includes(index)
            })
            currentTryTimes++
            return handleAllReq(tryTimes, ...arr)
          } else {
            v.forEach(item => {
              if (item.status === 'rejected') {
                res.error.push(item.reason.message)
              }
            })
            resolve(res)
          }
        }
      })
      pa.catch(r => {
        console.warn(r)
      })
    })(tryTimes, ...args)
  })
}

let req1 = () => axios.get('http://127.0.0.1:3007/famoussaying')
let req2 = () => axios.get('http://127.0.0.1:3007/famoussaying')
let req3 = () => axios.get('http://127.0.0.1:3007/famoussaying')

//使用 bigReq
bigReq(3, req1, req2, req3).then(res => {
  console.log(res);
}).catch(err => {
  console.error('Error occurred:', err);
});